package com.pine.common.util.user;

import com.alibaba.ttl.TransmittableThreadLocal;

/**
 * 用户信息工具类
 *
 * @author pine manage
 * @since 2024-08-12
 */
public class UserUtil {
    private static final ThreadLocal<UserInfo> USER_ENTITY_THREAD_LOCAL = new TransmittableThreadLocal<>();

    private UserUtil() {
    }

    public static long currentAccountId() {
        UserInfo currentUser = get();
        return currentUser == null ? -1L : get().id();
    }

    public static UserInfo get() {
        return USER_ENTITY_THREAD_LOCAL.get();
    }

    public static  void set(UserInfo user) {
        USER_ENTITY_THREAD_LOCAL.set(user);
    }

    public static void remove() {
        USER_ENTITY_THREAD_LOCAL.remove();
    }
}
